<?php

namespace app\common\model;

use think\Model;

/**
 * 邮箱验证码
 */
class People Extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    // 追加属性
    protected $append = [
    ];
    public function people($openid){
        $res = $this->where('openid',$openid)->find();
        return $res;
    }
    public function add_update_user($data){
        $count = $this->where('openid',$data['openid'])->count();
        if($count > 0){
            $arr = array(
                'name'  => $data['name'],
                'image' => $data['img']
            );
            if($data['people_id'] > 0){
                $arr['type'] = $data['type'] < 4 ? $data['type'] + 1 : 4;
                $arr['people_id'] = $data['people_id'];
            }
            $res = $this->where('openid',$data['openid'])->update($arr);
        }else{
            $arr = array(
                'name'  => $data['name'],
                'openid'=> $data['openid'],
                'image' => $data['img'],
                'createtime'=> time()
            );
            if($data['people_id'] > 0){
                $arr['type'] = $data['type'] < 4 ? $data['type'] + 1 : 4;
                $arr['people_id'] = $data['people_id'];
            }else{
                $arr['type'] = 4;
            }
            $res = $this->insert($arr);
        }
        return $res;
    }
}
